package com.project2.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project2.bean.CommonData;
import com.project2.bean.PageBo;
import com.project2.bean.po.MarketFootprint;
import com.project2.bean.po.MarketFootprintExample;
import com.project2.bean.po.MarketGoods;
import com.project2.bean.vo.BeanInListInDataInWxFootprintListVo;
import com.project2.mapper.MarketFootprintMapper;
import com.project2.mapper.MarketGoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangminghui
 * @date 2022/8/15
 */
@Service
public class WxFootprintServiceImpl2 implements WxFootprintService {
    @Autowired
    MarketFootprintMapper marketFootprintMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public CommonData<BeanInListInDataInWxFootprintListVo> queryDataInWxFootprintListVo(PageBo pageBo, Integer userId) {
        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());
//        MarketFootprintExample example = new MarketFootprintExample();
//        if (pageBo.getSort() != null && pageBo.getOrder() != null) {
//            example.setOrderByClause(pageBo.getSort() + " " + pageBo.getOrder());
//        }
//        // todo：这里传入的sort、order都为null，需要自己手动设计排序
//        pageBo.setSort("add_time");
//        pageBo.setOrder("desc");
//        example.setOrderByClause(pageBo.getSort() + " " + pageBo.getOrder());
//        MarketFootprintExample.Criteria criteria = example.createCriteria();
//        criteria.andDeletedEqualTo(false);
//        criteria.andUserIdEqualTo(userId);
//        List<MarketFootprint> footprintList = marketFootprintMapper.selectByExample(example);

        List<MarketFootprint> footprintList = marketFootprintMapper.selectByCondition(userId);

        List<BeanInListInDataInWxFootprintListVo> beans = new ArrayList<>();
        for (MarketFootprint marketFootprint : footprintList) {
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(marketFootprint.getGoodsId());
            BeanInListInDataInWxFootprintListVo vo = new BeanInListInDataInWxFootprintListVo();
            vo.setBrief(marketGoods.getBrief());
            vo.setAddTime(marketFootprint.getAddTime());
            vo.setGoodsId(marketGoods.getId());
            vo.setId(marketFootprint.getId());
            vo.setName(marketGoods.getName());
            vo.setPicUrl(marketGoods.getPicUrl());
            vo.setRetailPrice(marketGoods.getRetailPrice());
            beans.add(vo);
        }
        PageInfo<BeanInListInDataInWxFootprintListVo> pageInfo = new PageInfo<>(beans);

        return CommonData.data(pageInfo);
    }

    @Override
    public void deleteFootprintById(Integer id) {
        MarketFootprint marketFootprint = new MarketFootprint();
        marketFootprint.setId(id);
        marketFootprint.setDeleted(true);
        marketFootprintMapper.updateByPrimaryKeySelective(marketFootprint);
    }

}
